<template>
	<view class="page">
		<view class="common-wrap tp-w690">
			<view class="input-wrap">
				<view class="type">姓名</view>
				<view class="input">
					<input type="text" v-model="updata.name" placeholder="请输入姓名"/>
				</view>
			</view>
			<view class="input-wrap">
				<view class="type">手机号</view>
				<view class="input">
					<input type="number" v-model="updata.phone" maxlength="11" placeholder="请输入手机号"/>
				</view>
			</view>
			<view class="input-wrap">
				<view class="type">证件号码</view>
				<view class="input">
					<input type="idcard" v-model="updata.ID" maxlength="18" placeholder="请输入身份证号码"/>
				</view>
			</view>
			<!-- <view class="ID-wrap">
				<view class="type">身份证</view>
				<view class="ID_content">
					<view class="ID_positive ID" @click="handleUploadIDImgPositive('IDImgPositive')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>上传身份证正面</text>
						</view>
						<image class="ID_img" :src="updata.IDImgPositive" mode="scaleToFill"></image>
					</view>
					<view class="ID_negative ID" @click="handleUploadIDImgPositive('IDImgNegative')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>上传身份证反面</text>
						</view>
						<image class="ID_img" :src="updata.IDImgNegative" mode="scaleToFill"></image>
					</view>
					<view class="ID_handheld ID" @click="handleUploadIDImgPositive('IDImgHandheld')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>手持身份证照片</text>
						</view>
						<image class="ID_img" :src="updata.IDImgHandheld" mode="scaleToFill"></image>
					</view>
				</view>
			</view> -->
			
			<view class="ID-wrap">
				<view class="type">驾驶证</view>
				<view class="ID_content">
					<view class="ID_positive ID" @click="handleUploadIDImgPositive('driverLicense')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png" mode=""></image>
							</view>
							<text>上传驾驶证照片</text>
						</view>
						<image class="ID_img" :src="updata.driverLicense" mode="scaleToFill"></image>
					</view>
				</view>
			</view>
		</view> 
		
		<view class="common-btn" @click="handleConfirmAddPilot">确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				updata: {
					name: '',
					phone: '',
					ID: '',
					IDImgPositive: '', // 身份证正面照
					IDImgNegative: '', // 身份证反面照
					IDImgHandheld: '', // 身份证手持正面照
					driverLicense: '' // 驾驶证
				},
				driverInfo: {}
			};
		},
		onLoad(opt) {
			this.id = opt && opt.id
			if (this.id !== 'undefined' && this.id) {
				uni.setNavigationBarTitle({
					title: '编辑驾驶员'
				})
				this.getDriverDetail(this.id)
			}
		},
		methods: {
			handleUploadIDImgPositive(key) {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera '],
					success: ({tempFilePaths}) => {
						uni.uploadFile({
							url: this.$baseUrl + this.$api.uploadFile,
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								let uploadData = JSON.parse(uploadFileRes.data)
								this.$set(this.updata, key, uploadData.url)
							}
						})
					}
				});
			},
			handleConfirmAddPilot() {
				let {name, phone, ID, IDImgPositive, IDImgNegative, IDImgHandheld, driverLicense} = this.updata
				if(!name.trim()) return uni.showToast({title: '请输入姓名',icon: 'none'});
				if(!phone.trim()) return uni.showToast({title: '请输入手机号',icon: 'none'});
				if(!ID.trim()) return uni.showToast({title: '请输入证件号码',icon: 'none'});
				// if(!IDImgPositive) return uni.showToast({title: '请上传身份证正面',icon: 'none'});
				// if(!IDImgNegative) return uni.showToast({title: '请上传身份证反面',icon: 'none'});
				// if(!IDImgHandheld) return uni.showToast({title: '请上传手持身份证照片',icon: 'none'});
				if(!driverLicense) return uni.showToast({title: '请上传驾驶证照片',icon: 'none'});
				
				// this.updata.id = this.getRandomInt(1,99999)
				// this.$store.commit('upRiderList', this.updata)
				
				let api = this.$api.addDriver
				let updata = {name,phone,idNumber:ID,front:IDImgPositive,back:IDImgNegative,handle:IDImgHandheld,carImg:driverLicense}
				if (this.id && this.id !== 'undefined') {
					api = this.$api.editDriver
					updata.id = this.id
				}
				this.$http.post(api, updata).then(res => {
					if (res.code === 200) {
						uni.navigateBack({delta: 1});
						
						let arr_local = this.$store.getters.getRiderList
						let arr_net = []
						this.$http.post(this.$api.getRentDriverList).then(res => { // 后台获取驾驶员人最新信息
							if (res.code === 200) {
								res.data.map(item => {
									item.ID = item.idNumber
									item.choosed = false
								})
								arr_net = res.data
								arr_local.forEach((it_l, in_l) => {
									arr_net.forEach((it_n, in_n) => {
										if (it_l.id===it_n.id) {
											this.$store.commit('upRiderList', it_n) // 更新本地驾驶员人信息
										}
									})
								})
							}
						})
					}
				})
			},
			getRandomInt(min, max) {
			  min = Math.ceil(min);
			  max = Math.floor(max);
			  return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
			},
			getDriverDetail() {
				if (!this.id) return
				this.$http.get(this.$api.getRentDriverDetail, {id: this.id}).then(res => {
					if (res.code === 200) {
						this.driverInfo = res.data
						let {back, carImg, front, handle, idNumber, name, phone} = res.data
						this.updata.IDImgNegative = back
						this.updata.IDImgPositive = front
						this.updata.IDImgHandheld = handle
						this.updata.driverLicense = carImg
						this.updata.name = name
						this.updata.phone = phone
						this.updata.ID = idNumber
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.page {
	/deep/.input-placeholder {
		font-size: 30rpx;
		font-weight: normal;
		color: #ccc;
	}
}
.common-wrap {
	border-radius: 12rpx;
	background-color: #fff;
	padding: 0 30rpx;
	box-sizing: border-box;
	margin-top: 20rpx;
	.input-wrap {
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1px solid #ebebeb;
		font-size: 30rpx;
		&:last-child {
			border: none;
		}
		&.remark {
			height: 200rpx;
			align-items: flex-start;
			padding: 30rpx 0;
			box-sizing: border-box;
			.textarea {
				flex: 1;
				height: 160rpx;
			}
		}
		.l-wrap {
			display: flex;
			align-items: center;
			.icon {
				display: inline-block;
				height: 30rpx;
				width: 30rpx;
				margin-right: 20rpx;
			}
			.terms {
				color: $blue;
			}
		}
		.pirce {
			color: $red;
			padding-right: 10rpx;
		}
		.ul_service {
			display: flex;
			.li_service {
				display: flex;
				align-items: center;
				font-size: 30rpx;
				margin-left: 80rpx;
				.txt {
					margin-left: 20rpx;
				}
			}
		}
		.type {
			font-size: 30rpx;
		}
		.input {
			flex: 1;
			text-align: right;
			font-size: 30rpx;
			display: flex;
			input {
				padding-right: 6rpx;
			}
		}
	}
	.ID-wrap {
		.type {
			height: 100rpx;
			line-height: 100rpx;
			font-size: 30rpx;
		}
		.ID_content {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			.ID {
				margin-bottom: 20rpx;
				position: relative;
				width: 296rpx;
				height: 188rpx;
				border: 1px dashed #ccc;
				border-radius:12rpx;
				.bg-wrap {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: space-evenly;
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					font-size: 26rpx;
					color: #ccc;
					z-index: 1;
					.camer-icon-wrap {
						height: 64rpx;
						width: 64rpx;
						border-radius: 50%;
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}
				&.ID_handheld {
					width: 220rpx;
					height: 280rpx;
					.bg-wrap {
						padding: 50rpx 0;
						box-sizing: border-box;
					}
				}
				.ID_img {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					z-index: 2;
				}
			}
		}
	}
}

.common-btn {
	width: 580rpx;
	margin: 40rpx auto 58rpx;
	box-shadow: none;
}
</style>
